<?php
/*
Template Name: Post Buku Baru
*/
?>

<?php


  
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

		$descriptionarray = $_POST['hidallbabtab'];
		$descriptionparse = explode ("|",$descriptionarray);
		$counter = 0;
		$pbuku = 0;
		$pbab = 0;
	// Do some minor form validation to make sure there is content
	if (isset($_POST['publish'])) {
 
		foreach ($descriptionparse as $description){
		if ($description == "description0"){
			// untuk sinopsis
				$tags = $_POST['post_tags'];
			$new_post = array(
				'post_title'	=>	$_POST['title0'],
				'post_content'	=>	$_POST[$description],
				'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
				'tags_input'	=>	array($tags),
				'post_status'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
				'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
				);
			$pbuku = wp_insert_post($new_post);
			wp_set_post_tags($pbuku, $_POST['post_tags']);
		}
		else {
		   $titleid = substr($description,11); // mendapatkan id textbox judul
		//untuk bab
			$titlea = "title". $titleid;
			$title ="";
			if (empty($_POST[$titlea])){
			$title = "Bab " . (string)$counter;
			}
			else {$title =$_POST[$titlea]; }
			$new_chapter = array(
				'post_title'	=>	$title,
				'post_content'	=>	$_POST[$description],
				'post_status'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
				'post_type'	=>	'chapter',  //'post',page' or use a custom post type if you want to
				);
			$pbab = wp_insert_post($new_chapter);
			add_post_meta($pbuku, 'Chapter', $pbab, false);
			add_post_meta($pbab, 'number', $counter, true);
				
			}
			$counter = $counter + 1;
		}
	}
	
	//DRAFT!!	
	else {
	foreach ($descriptionparse as $description){
		if ($description == "description0"){
			// untuk sinopsis
				$tags = $_POST['post_tags'];
			$new_post = array(
				'post_title'	=>	$_POST['title0'],
				'post_content'	=>	$_POST[$description],
				'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
				'tags_input'	=>	array($tags),
				'post_status'	=>	'draft',           // Choose: publish, preview, future, draft, etc.
				'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
				);
			$pbuku = wp_insert_post($new_post);
			wp_set_post_tags($pbuku, $_POST['post_tags']);
		}
		else {
		//untuk bab
			$titleid = substr($description,11); // mendapatkan id textbox judul
			$titlea = "title" . $titleid;
			$title ="";
			if (empty($_POST[$titlea])){
			$title = "Bab " . (string)$counter;
			}
			else {$title =$_POST[$titlea]; }
			
			$new_chapter = array(
				'post_title'	=>	$title,
				'post_content'	=>	$_POST[$description],
				'post_status'	=>	'draft',           // Choose: publish, preview, future, draft, etc.
				'post_type'	=>	'chapter',  //'post',page' or use a custom post type if you want to
				);
			$pbab = wp_insert_post($new_chapter);
			add_post_meta($pbuku, 'Chapter', $pbab, false);
			add_post_meta($pbab, 'number', $counter, true);
				
			}
			$counter = $counter + 1;
		}
	
	}
	$link2 = get_site_url() . "/daftarcerita";
	wp_redirect( $link2 );
}	
	
do_action('wp_insert_post', 'wp_insert_post');

?>

<?php get_header(); ?>

    <div class="col-md-12 column content">
	<div class="row clearfix">
		
		<?php if (is_user_logged_in()) { get_template_part( 'kolompertama2' ); 
	echo "<div class='col-md-9 column' >";  }
else
		{ 
			echo "<div class='col-md-12 column' > ";
		}?> <!-- untuk kolom kedua yang dibagi 2 -->
		<div class="row clearfix">
		
				<div class="col-md-12 column">
				 <?php if(is_user_logged_in()){?>
                	<h8 class="judulprofil">Tulis Cerita</h8
					><?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <br/>
                    <br/>
					<?php 
					        if (!empty($error)) {
	                            echo  "<strong>Tidak dapat memproses permintaan Anda, mohon isi dahulu : " . $error . ""  ;
	                        } elseif (!empty($success)) {
	                            echo $success ;
	                        }
	                    ?>
					<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
                    <div>
                  
                    <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Judul</div>
                            <div class="col-md-9 ">
                                <div class="col-sm-12">
                                    <input class="form-control tinggitextbox" id="title0" type="text" name="title0" placeholder="Judul Cerita Wajib diisi" 
									value="<?php if (!empty($_POST['title'])) echo $title ?>">
									
									
                                </div>
								
                            </div>
							<label id="errorjudulbuku" style="color:red; font-size: 10px;"></label>
                        </div>
                    	</div>
                    <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Kategori</div>
                            <div class="col-md-9 ">
                                <div class="dropdown padd5">
								<?php wp_dropdown_categories( 'show_option_none=Silakan Pilih Kategori&tab_index=10&hide_empty=0&class="dropdowncat"&exclude=2,4,1' ); ?>
                                  </div>
                            </div>
							<label id="errorkatbuku" style="color:red; font-size: 10px;"></label>
                        </div>
                    </div>
                    <br/>
					<!-- <div class="col-md-2">Sinopsis</div>
		
					<div  class="panel panel-default">
					
						 <textarea class="kotakexcerpt" name="excerpt" id="excerpt" >
						 <?php 
                       // if (!empty($_POST['excerpt'])) { echo htmlspecialchars($_POST['excerpt']);} ?>
                        </textarea>
						</div>
						</br> -->
						</br>
                      <!--  <div class="btn-group">
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-right"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-center"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-left"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-height"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-bold"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-italic"></span></button>
                              <div class="btn-group">
                                  <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">
                                    Times New Roman <span class="caret"></span>
                                  </button>
                                      <ul class="dropdown-menu" role="menu">
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">Calibri</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">Arial</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">San Serif</a></li>
                                      </ul>
                              </div>
                              <div class="btn-group">
                                  <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">
                                    10px <span class="caret"></span>
                                  </button>
                                      <ul class="dropdown-menu" role="menu">
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">12px</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">14px</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">16px</a></li>
                                      </ul>
                              </div>
                              
                              </div>-->
                              <br/>
							  <a id="AddTabBab" OnClick="DoAddBab();" onmouseover="this.style.cursor='pointer'"><span class="glyphicon glyphicon-plus" style="color:#006600"></span>&nbsp;Tambah Bab</a><br/>
                        <div class="tabbable" style=" padding-top:6px;" id="tabtable">
                          <ul class="nav nav-tabs">
                            <li class="activetab" id="li0"><span ><a class="showtab" href="#pane0" data-toggle="tab">Sinopsis</a></span></li>
                                                    
                          </ul>
                          <div class="tab-content" id="tab-content">
                            <div id="pane0" class="tab-pane active">
                              <textarea class="kotaktext" name="description0" id="description0" >     </textarea> <!--Jangan hapus id atau name textaera ini!!, kalau diganti script gak jalan-->
                            </div>
           

                          </div><!-- /.tab-content -->
                        </div><!-- /.tabbable -->
                   <label id="errorsinopsisbuku" style="color:red; font-size: 10px;"></label>
                       </div>
                       </div>
                       
                    <br/>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="input-group input-group-sm padd6">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-tags"></span></span>
                                <input type="text" class="form-control" name="post_tags" id="post_tags" placeholder="Tag wajib diisi, pisahkan tag dengan tanda ',' atau ';'" />
                            </div>
                        </div>
						<label id="errortagbuku" style="color:red; font-size: 10px;"></label>
						
                    </div>
                    </div>
                    <br/>
					
                    <a class="btn btn-sm" tabindex="40" OnClick="DoDraftBuku();" >Simpan Konsep</a>
					<input type="submit" value="Simpan Konsep" tabindex="40" id="draftbuku" name="draftbuku" class="btn btn-sm" style="display:none;">
	   				<input type="hidden" name="action" value="draft_post" /></input>
					<a class="btn btn-sm" tabindex="40" OnClick="DoPublishBuku();">Simpan dan Tayangkan</a>
					<input type="hidden" name="hidallbabtab" value="" id="hidallbabtab" />
					<input type="hidden" name="hidallbabtabjudul" value="" id="hidallbabtabjudul" />
					<input type="submit" value="Simpan dan Tayangkan" tabindex="40" id="publishbuku" name="publish" class="btn btn-sm" style="display:none;">
	   				<input type="hidden" name="action" value="publish_post" /></input>
                    
					<?php wp_nonce_field( 'new-post' ); ?>
                  
                    <!--<button type="submit" class="btn btn-sm">Batal</button>-->
					</form><?php endwhile;} 
					/*
					untuk form login jika belum masuk/terdaftar
					
					*/
					else
					{?>
                    
                  
                  
					<br/>
				<strong style="font-family: Satisfy, cursive; font-size:24px; color:#000; "><center>	Bergabunglah dengan komunitas menulis di bawah naungan Gramedia Pustaka Utama. </center></strong><br/>
                	<strong style="font-family: Satisfy, cursive; font-size:28px; color:#FF0000; "><center>	GRATIS!!...</center></strong>
                    <br/><br/>
                    
                    <div class="row">
                    <div class="col-md-2 column"></div>
					  <div class="col-md-8 column" style="min-height:250px;" ><!-- untuk kolom pertama -->
					<?php 	 ?> 
                   
                                <div class="panel-group" id="panel-661231">
                                    <div class="panel panel-default">
                                        <div class="panel-heading">
                                            <table style="width:100%">
                                                <tr>
                                                    <td style="width:45%; text-align:center; border-right:thin solid #CCCCCC">
                                                    <a class="panel-title" data-toggle="collapse" data-parent="#panel-661231" href="#login">Masuk</a></td>
                                                    <td style="width:50%; text-align:center; ">
                                                    <a class="panel-title" data-toggle="collapse" data-parent="#panel-661231" href="#register">Daftar</a></td>
                                                </tr>
                                            </table>
                                        </div>
										<?php get_template_part('login2');?>
                                        
											<?php get_template_part('register');?>
                                     
                                    </div>
                                    
                                </div>
                                
								</div>
                  
					<?php }					?>
                    </div>
           			<div class="col-md-2 column"></div>
                    
	    	
            </div>
	   
       </div><!-- end row clearfix -->
 <!--end col-md-12 column content-->
	<!-- end untuk content -->
   
    
    <!-- untuk footer -->

<?php get_footer(); ?>
